//
//  ZHFindRentMsgViewController.m
//  sc-cyan-ios
//
//  Created by lj on 2018/1/3.
//  Copyright © 2018年 greenbuilding. All rights reserved.
//

#import "ZHFindRentMsgViewController.h"
#import "ZHFindRentMsgFiltrateViewController.h"
#import "ZHFindRentMsgDetailsViewController.h"

#import "ZHSearchNavView.h"
#import "ZHFindRentMsgTableViewCell.h"

typedef enum : NSUInteger {
    FindRentMsgTimeTag = 10,//时间最新
    FindRentMsgDistanceTag,//距离最近
    FindRentMsgFilter,//筛选
} FindRentMsgBtnTag;

static NSString *const cellID = @"fndRentMsgTableViewCell";
static NSString *const footerID = @"fndRentMsgfooter";

@interface ZHFindRentMsgViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger lastSelTag;
}

//顶部父视图
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//搜索框
@property (nonatomic,strong) ZHSearchNavView *searchView;

@end

@implementation ZHFindRentMsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpView];
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar addSubview: self.searchView];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.searchView removeFromSuperview];
    
}

- (ZHSearchNavView *)searchView{
    if (!_searchView) {
        _searchView = [ZHSearchNavView initWithXib];
        _searchView.autoresizingMask = UIViewAutoresizingNone;
        _searchView.frame = self.navigationController.navigationBar.bounds;
        MJWeakSelf
        _searchView.popAction = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        _searchView.searchAction = ^(NSString *text) {
            DLog(@"--%@",text);
        };
        _searchView.loction = @"  贵阳";
    }
    return _searchView;
}

- (void)setUpView{
    //默认选中时间最新
    UIButton *btn = [self.topView viewWithTag:10];
    lastSelTag =10;
    [self topTagBtnAction:btn];
    
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
}

#pragma mark <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZHFindRentMsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZHFindRentMsgTableViewCell class]) owner:nil options:nil ] firstObject];
    }
    return cell;
}

#pragma mark <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UITableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footerID];
    
    if (!footer) {
        footer = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:footerID];
        footer.contentView.backgroundColor = UIColorFromRGB(0xf7f7f7);
    }
    
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, .5)];
    top.backgroundColor = [UIColor gb_line_d4d4d4];
    [footer.contentView addSubview:top];
    
    UIView *bottom = [[UIView alloc] initWithFrame:CGRectMake(0, [self tableView:tableView heightForFooterInSection:section] - .5, Main_Screen_Width,.5)];
    bottom.backgroundColor = [UIColor gb_line_d4d4d4];
    [footer.contentView addSubview:bottom];
    
    return footer;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZHFindRentMsgDetailsViewController *details = [[ZHFindRentMsgDetailsViewController alloc] init];
    
    [self.navigationController showViewController:details sender:nil];
    
}

//顶部按钮触发事件
- (IBAction)topTagBtnAction:(UIButton *)sender {
    if (sender.tag == FindRentMsgFilter) {
        ZHFindRentMsgFiltrateViewController *filtrateVC = [[ZHFindRentMsgFiltrateViewController alloc] init];
        
        filtrateVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
            
            filtrateVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        }
        else{
            
            self.modalPresentationStyle=UIModalPresentationCurrentContext;
        }
        
        [self presentViewController:filtrateVC animated:NO completion:^{
            
        }];
       
    }else{
        sender.selected = YES;
        
        if (sender.tag != lastSelTag) {
            
            UIButton *lastBtn = [self.topView viewWithTag:lastSelTag];
            lastBtn.selected = NO;
            lastSelTag = sender.tag;
            
        }
    }
}

@end
